package com.me.services.db;

import com.me.dao.IItemDao;
import com.me.dao.normal.NormalItemDao;
import com.me.model.Item;
import com.me.model.User;
import com.me.model.normal.NormalItem;
import com.me.services.IItemService;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

public class ItemDbService implements IItemService<User, NormalItem> {
	private IItemDao<NormalItem> iDao;
	private static boolean initial;
	
	public ItemDbService() {
		this.iDao = new NormalItemDao();
	}
	
	@Override
	public void addItem(NormalItem item) {
		iDao.addItem(item);
	}
	
	@Override
	public void initial(User u) {
		if (initial) return;
		Collection<NormalItem> items = getAll();
		for (NormalItem i : items) {
			iDao.initialItem(u.getId(), i.getId());
		}
		initial = true;
	}
	
	@Override
	public NormalItem getItem(int id) {
		if (id > getItemCount() - 1) {
			id = getItemCount() - 1;
		} else if (id < 0) {
			id = 0;
		}
		return iDao.getItem(id);
	}
	
	@Override
	public NormalItem getItemFromUser(User u, int itemId) {
		if (itemId > getItemCount() - 1) {
			itemId = getItemCount() - 1;
		} else if (itemId < 0) {
			itemId = 0;
		}
		return iDao.getItemFromUser(u.getId(), itemId);
	}
	
	@Override
	public Collection<NormalItem> getAll() {
		return iDao.getAll();
	}
	
	@Override
	public Collection<NormalItem> getAllFromUser(User u) {
		return iDao.getAllFromUser(u.getId());
	}
	
	@Override
	public int getItemCount() {
		return iDao.getItemCount().intValue();
	}
	
	@Override
	public void record(User user, NormalItem item, int chooseId) {
		iDao.recordUserChoose(user.getId(), item.getId(), chooseId);
	}
	
	@Override
	public int getScore(User u) {
		int score = 0;
		for (NormalItem i : getAllFromUser(u)) {
			if (i.isRight()) {
				score += i.getScore();
			}
		}
		return score;
	}

//	@Override
//	public void addItem(String title, String[] content, int score, String answer) {
//		addItem(title, content, score, answer, false);
//	}
//
//	@Override
//	public void addItem(String title, String[] content, int score, String answer, boolean multiple) {
//		iDao.addItem(title, content, score, answer, multiple);
//	}
//
//	@Override
//	public void initial(TUser u) {
//		if (initial) return;
//		try {
//			Collection<TItem> items = getAll();
//			for (TItem i : items) {
//				iDao.initialItem(
//						u.getClass().getMethod("getId").invoke(u),
//						i.getClass().getMethod("getId").invoke(i)
//				);
//			}
//		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//			e.printStackTrace();
//		}
//		initial = true;
//	}
//
//	@Override
//	public Collection<TItem> getAll() {
//		return iDao.getAll();
//	}
//
//	@Override
//	public Collection<TItem> getAllFromUser(TUser u) {
//		try {
//			return iDao.getAllFromUser(u.getClass().getMethod("getId").invoke(u));
//		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//
//	@Override
//	public TItem getItem(int id) {
//		if (id > getItemCount() - 1) {
//			id = getItemCount() - 1;
//		} else if (id < 0) {
//			id = 0;
//		}
//		return iDao.getItem(id);
//	}
//
//	@Override
//	public TItem getItemFromUser(TUser u, int itemId) {
//		try {
//			if (itemId > getItemCount() - 1) {
//				itemId = getItemCount() - 1;
//			} else if (itemId < 0) {
//				itemId = 0;
//			}
//			return iDao.getItemFromUser(u.getClass().getMethod("getId").invoke(u), itemId);
//		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//
//	@Override
//	public int getItemCount() {
//		return iDao.getItemCount().intValue();
//	}
//
//	@Override
//	public void record(TUser u, TItem i, int chooseId) {
//		try {
//			iDao.recordUserChoose(
//					u.getClass().getMethod("getId").invoke(u),
//					i.getClass().getMethod("getId").invoke(i),
//					chooseId
//			);
//		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//			e.printStackTrace();
//		}
//	}
//
//	@Override
//	public int getScore(TUser u) {
//		try {
//			int score = 0;
//			for (TItem i : getAllFromUser(u)) {
//				Method isRight = i.getClass().getMethod("isRight");
//				Method getScore = i.getClass().getMethod("getScore");
//				if ((boolean) isRight.invoke(i)) {
//					score += (int) getScore.invoke(i);
//				}
//			}
//			return score;
//		} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//			e.printStackTrace();
//		}
//		return 0;
//	}

//    public static void main(String[] args) {
//        ItemMemoryService iServ = new ItemMemoryService();
//        iServ.addItem("1+1=（）",new String[]{"1","2","3"},2,"b");
//        iServ.addItem("2*=89()",new String[]{"177","178","179","245"},2,"b");
//        iServ.addItem("123*=1()",new String[]{"122","121","123","-123"},2,"c");
//        iServ.addItem("含有正确的句子",new String[]{"正确了","确证了","了正确","确了正"},5,"ac",true);
//        iServ.addItem("选择所有的数字",new String[]{"1","一","3","5"},5,"acd",true);
//    }
}
